package com.etouch.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etouch.security.exception.ExceptionEnum;
import com.etouch.security.exception.ProjectException;
import com.etouch.security.mapper.SysUserMapper;
import com.etouch.security.pojo.dto.SysUserDTO;
import com.etouch.security.pojo.entity.SysUser;
import com.etouch.security.pojo.searchargs.SearchSysUserArgs;
import com.etouch.security.service.SysUserService;
import com.etouch.security.util.BeanHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author chenyunchang
 * @since 2020-10-28
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> selectUserList(SearchSysUserArgs args) {
        //创建条件查询器
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        //获取筛选参数
        Integer id = args.getId();
        String username = args.getUsername();
        String realName = args.getRealName();
        //构造条件查询器
        queryWrapper.lambda().eq(id != null, SysUser::getId, id)
                .eq(StringUtils.isNotBlank(username), SysUser::getUsername, username)
                .eq(StringUtils.isNotBlank(realName), SysUser::getRealName, realName);
        return sysUserMapper.selectList(queryWrapper);
    }

    @Override
    public SysUserDTO getSimpleUser(Integer id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null) {
            throw new ProjectException(ExceptionEnum.USER_NOT_FOUND);
        }
        //将密码置空
        sysUser.setPassword(null);
        return BeanHelper.copyProperties(sysUser, SysUserDTO.class);
    }

    @Override
    public SysUserDTO getUserDetail(SearchSysUserArgs args) {
        SysUserDTO sysUserDTO = sysUserMapper.getUserDetailByArgs(args);
        //将密码置空
        sysUserDTO.setPassword(null);
        return sysUserDTO;
    }

    @Override
    public SysUserDTO getUserByUserName(String userName) {
        return sysUserMapper.getUserByUserName(userName);
    }

    @Override
    public SysUserDTO getUserByPhone(String phone) {
        SearchSysUserArgs searchSysUserArgs = new SearchSysUserArgs();
        searchSysUserArgs.setPhone(phone);
        return sysUserMapper.getUserDetailByArgs(searchSysUserArgs);
    }

    @Override
    public void delete(Integer id) {
        sysUserMapper.deleteById(id);
    }

    @Override
    public void saveSysUser(SysUserDTO sysUserDTO) {
        SysUser sysUser = BeanHelper.copyProperties(sysUserDTO, SysUser.class);
        sysUserMapper.insert(sysUser);
    }

    @Override
    public void loginByPhoneAndCode(String phone, String code) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getPhone, phone)
                .eq(SysUser::getStatus, 1);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUserDTO getUserByPhone2(String phone) {
        //TODO 临时测试, 这里将值写死
        SysUserDTO sysUserDTO = new SysUserDTO();
        sysUserDTO.setId(1);
        sysUserDTO.setUsername("test");
        sysUserDTO.setPassword("123");
        sysUserDTO.setPhone("110");
        sysUserDTO.setRealName("张三");
        sysUserDTO.setStatus(1);
        sysUserDTO.setLastLoginTime(LocalDateTime.now());
        //2.假设上面是数据库中查出来的, 和用户输入的进行比对
        if (!sysUserDTO.getPhone().equals(phone)) {
            throw new ProjectException(400, "用户不存在");
        }
        return sysUserDTO;
    }
}
